package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private ReportMapper reportMapper;


    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;



    /**
     * 统计营业数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {
        // 获取当日时间
        Map map = new HashMap();
        map.put("end", endTime);
        map.put("begin", beginTime);
        System.out.println(map);

        // 统计今日新增用户数
        Integer newUser = reportMapper.userStatistics(map);

        // 统计今日订单总数
        Integer totalOrderCount = reportMapper.ordersStatistics(map);
        // 添加查询条件，指定订单状态为“已完成”
        map.put("status", Orders.COMPLETED);
        // 统计今日订单完成数
        Integer validOrderCount = reportMapper.ordersStatistics(map);
        // 计算今日有效订单率
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            //计算订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }

        // 统计今日营业额
        Double turnover = reportMapper.turnoverStatistics(map);
        if (turnover == null){
            turnover = 0.0;
        }

        // 统计平均客单价
        Double averageOrderAmount = 0.0;
        if(validOrderCount != 0){
            //计算平均客单价
            averageOrderAmount = turnover / validOrderCount;
        }

        // 封装返回结果
        BusinessDataVO businessDataVO = BusinessDataVO.builder().turnover(turnover).
                validOrderCount(validOrderCount).
                orderCompletionRate(orderCompletionRate).
                unitPrice(averageOrderAmount).
                newUsers(newUser).build();
        System.out.println(businessDataVO);
        return businessDataVO;

    }

    /**
     * 订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders(LocalDateTime beginTime, LocalDateTime endTime) {
        // 获取当日时间
        Map map = new HashMap();
        map.put("end", endTime);
        map.put("begin", beginTime);
        // 统计订单总量
        Integer totalOrderCount = reportMapper.ordersStatistics(map);
        // 统计订单状态为待接单的数量
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer TO_BE_CONFIRMED = reportMapper.ordersStatistics(map);
        // 统计订单状态为派送中的数量
        map.put("status", Orders.DELIVERY_IN_PROGRESS);
        Integer DELIVERY_IN_PROGRESS = reportMapper.ordersStatistics(map);
        // 统计订单状态为已完成的数量
        map.put("status", Orders.COMPLETED);
        Integer COMPLETED = reportMapper.ordersStatistics(map);
        // 统计订单状态为已取消的数量
        map.put("status", Orders.CANCELLED);
        Integer CANCELLED = reportMapper.ordersStatistics(map);


        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder().allOrders(totalOrderCount).
                waitingOrders(TO_BE_CONFIRMED).
                deliveredOrders(DELIVERY_IN_PROGRESS).
                completedOrders(COMPLETED).
                cancelledOrders(CANCELLED).build();

        return orderOverViewVO;
    }

    /**
     * 套餐管理数据
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        // 统计停售套餐总数
        Integer disableSetmealsTotal = setmealMapper.getSetmealsStatus(StatusConstant.DISABLE);
        // 统计起售套餐总数
        Integer enableSetmealsTotal = setmealMapper.getSetmealsStatus(StatusConstant.ENABLE);

        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder().sold(enableSetmealsTotal).discontinued(disableSetmealsTotal).build();

        return setmealOverViewVO;
    }

    /**
     * 菜品管理数据
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        // 统计停售套餐总数
        Integer disableSetmealsTotal = dishMapper.getSetmealsStatus(StatusConstant.DISABLE);
        // 统计起售套餐总数
        Integer enableSetmealsTotal = dishMapper.getSetmealsStatus(StatusConstant.ENABLE);

        DishOverViewVO dishOverViewVO = DishOverViewVO.builder().sold(enableSetmealsTotal).discontinued(disableSetmealsTotal).build();

        return dishOverViewVO;
    }


//    private Map getMap() {
//        // 获取当日时间
//        LocalDate date = LocalDate.now();
//        LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
//        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
//        Map map = new HashMap();
//        map.put("end", endTime);
//        map.put("begin", beginTime);
//        return map;
//    }
}
